<template>
  <div class="order-paid" v-if="pageData.order_no">
    <div class="state-box">
      <div class="pic">
        <img
          class="img-size-100"
          :src="orderState[pageData.state].icon"
          alt=""
        />
      </div>
      <div class="txt">
        <div class="name">{{ orderState[pageData.state].title }}</div>
        <van-count-down
          v-if="pageData.state == 0"
          :time="pageData.expires_ms * 1000 + 1"
          format="mm:ss"
          use-slot
          @change.native="onCountDowChange"
        >
          <text class="order-status-countTime"
            >剩余{{ timeData.minutes }}:{{ timeData.seconds }}自动关闭</text
          ></van-count-down
        >
        <!-- <div>等待发货</div> -->
      </div>
      <div class="seat"></div>
    </div>

    <div class="info-box">
      <div class="address" v-if="pageData.book_name">
        <div class="top">
          <text>{{ pageData.book_name }}</text>
          <text class="tel">{{ pageData.book_phone }}</text>
        </div>
        <div class="bottom">{{ pageData.book_address }}</div>
        <div class="logistics" v-if="pageData.freight_no">
          <div class="left">
            {{ pageData.freight_company + " " }}{{ pageData.freight_no }}
          </div>
          <div class="copy-btn" @click="setClipboardData(pageData.freight_no)">
            复制
          </div>
        </div>
      </div>

      <div class="product-box">
        <a
          class="top"
          hover-class="none"
          :url="'/pages/shanghu/details?id=' + pageData.merchant.id"
        >
          <div class="pic">
            <img :src="pageData.merchant.logo" class="img-size-100" />
          </div>
          <div class="name">{{ pageData.merchant.name }}</div>
        </a>
        <a class="info" hover-class="none" :url="'/shop/productDetails/productDetails?id=' + pageData.product_id">
          <div class="pic">
            <img :src="pageData.product_cover_picture" class="img-size-100" />
          </div>
          <div style="flex:1">
            <div class="name">{{ pageData.product_title }}</div>
            <div class="specification">
              {{ pageData.product_specification_title }}
            </div>
          </div>

          <div class="price">
            <div class="money">
              <text></text>
              <text class="number">{{ pageData.product_sku_price_name }}</text>
            </div>
            <div class="amount">x{{ pageData.product_number }}</div>
          </div>
        </a>
        <div class="total-tips">
          共1件商品 ，合计
          <text class="number">{{ pageData.amount_name }}</text>
          <text v-if="pageData.freight > 0"
            >(含运费:{{ pageData.freight_name }})</text
          >
        </div>
      </div>

      <div class="list-box">
        <div class="item" v-if="pageData.remark">
          <div class="title">留言</div>
          <text class="remark">{{ pageData.remark }}</text>
        </div>

        <div
          class="item details"
          v-if="pageData.state !== 0 && pageData.state !== 4"
        >
          <div class="title">支付方式</div>
          <div>{{ pageData.pay_type_name }}</div>
        </div>
        <div
          class="item details"
          @click="showPayType = true"
          v-if="pageData.state == 0"
        >
          <div class="title">支付方式</div>
          <div style="display:flex;align-items: center;">
            <div style=" white-space: nowrap;">
              {{ payTypeText
              }}<block v-if="payType == 1"
                >(剩余{{ userInfo.amount_name }})</block
              >
            </div>
            <div class="arrow-icon"></div>
          </div>
        </div>
        <div class="item details" v-if="pageData.amount_coupon > 0">
          <div class="title">优惠券</div>
          <div>-{{ pageData.amount_coupon_name }}</div>
        </div>
        <div class="item details">
          <div class="title">总计</div>
          <div>
            美粉币
            <text class="number">{{ pageData.amount_paid_name }}</text>
          </div>
        </div>
      </div>
    </div>
    <div class="contact-box" @click="makePhoneCall(pageData.merchant.tel)">
      <text class="icon"></text>
      <text class="txt">联系商家</text>
    </div>
    <div class="cell-box">
      <a
        v-if="
          pageData.delivery_state == 0 &&
            pageData.is_card !== 1 &&
            (pageData.state == 2 || pageData.state == 3)
        "
        class="code-box"
        :url="
          pageData.state == 2
            ? '/pages/paymentCode/paymentCode?type=2&order_no=' +
              pageData.order_no
            : ''
        "
        hover-class="none"
      >
        <div>{{ pageData.state == 2 ? "出示核销码" : "已核销完成" }}</div>
        <div class="right">
          <div class="code" v-if="pageData.state == 2">
            <img src="https://media.wxcwy.com/mini/images/ewm_icon2@2x.png" class="img-size-100" />
          </div>
        </div>
      </a>
      <a
        :url="'/pages/coupon/coupon?orderNo=' + pageData.order_no"
        class="code-box blue"
        hover-class="none"
        v-if="pageData.state == 3 && pageData.is_card == 1"
      >
        <div style="display:flex; white-space: nowrap;"><text class="coup-icon"></text> 查看对应卡券</div>
        <div>
          <text style="color:#B2B2B2;font-size:28rpx;">小主快点我去激活使用吧</text>
          <text class="right"></text>
        </div>
      </a>
    </div>

    <div class="order-box">
      <div class="title">订单信息</div>
      <div class="main">
        <div class="left">
          <div class="txt">订单编号：{{ pageData.order_no }}</div>
          <div class="txt">创建时间：{{ pageData.created_at }}</div>
          <div class="txt" v-if="pageData.state == 4">
            取消时间：{{ pageData.end_time_name }}
          </div>
          <div v-if="pageData.pay_time_name != ''" class="txt">
            支付时间：{{ pageData.pay_time_name }}
          </div>
          <div class="txt" v-if="pageData.state == 3">
            完成时间：{{ pageData.end_time_name }}
          </div>
        </div>
        <div class="right">
          <div class="copy-btn" @click="setClipboardData(pageData.order_no)">
            复制
          </div>
        </div>
      </div>
    </div>

    <div class="footer-box" v-if="pageData.state == 0">
      <div class="main">
        <div class="btn" @click.stop="callOrder(pageData.order_no)">
          取消订单
        </div>
        <div class="btn pay" @click.stop="onPay(pageData.order_no)">
          去付款
        </div>
      </div>
    </div>
    <div
      class="footer-box"
      v-if="pageData.state == 2 && pageData.delivery_state == 2"
    >
      <div class="main">
        <div class="btn pay" @click="onReceipt(pageData.order_no, pageData.pay_type)">
          确认收货
        </div>
      </div>
    </div>
    <van-popup
      closeable
      class="pay-type-pop"
      :show="showPayType"
      custom-style="height:595rpx;padding: 0 30rpx;"
      @close.native="showPayType = false"
      position="bottom"
    >
      <van-cell>
        <view slot="title" class="model-title">
          请选择支付方式
        </view></van-cell
      >
      <van-cell
        center
        custom-class="cell-box"
        v-for="(item, index) in payTypeList"
        :key="index"
        @click="onSelectPayType(item.type)"
      >
        <text slot="icon" :class="item.icon + '-icon'"></text>
        <view slot="title" class="title">
          <div>
            {{ item.name
            }}<block v-if="item.type == 1">
              (剩余{{ userInfo.amount_name }})</block
            >
          </div>
          <div class="tips-t" v-if="item.tips && isPay">{{ item.tips }}</div>
        </view>
        <text
          slot="right-icon"
          class="radio-icon"
          :class="{ active: item.type == payType }"
        ></text>
      </van-cell>
    </van-popup>
  </div>
</template>

<script>
import { mapState } from "vuex";
import shbPay from "@/common/shb-pay.js";
import vanCountDown from "@/wxcomponents/vant/count-down/index";
import vanPopup from "@/wxcomponents/vant/popup/index";
import vanCell from "@/wxcomponents/vant/cell/index";
import vanCellGroup from "@/wxcomponents/vant/cell-group/index";
var productId;
export default {
  components: { vanCountDown, vanPopup, vanCell, vanCellGroup },
  data() {
    return {
      showPayType: false,
      payType: 0,
      payTypeList: [
        { name: "微信支付", icon: "wx", type: 2 },
        { name: "余币支付", icon: "ye", type: 1, tips: "余币不足" },
      ],
      pageData: {},
      orderState: {
        0: {
          title: "待付款",
          icon: "https://media.wxcwy.com/mini/images/dfk_img@2x.png",
        },
        1: {
          title: "待发货",
          icon: "https://media.wxcwy.com/mini/images/dfh_img@2x.png",
        },
        2: {
          title: "待确认",
          icon: "https://media.wxcwy.com/mini/images/dhx@2x.png",
        },
        3: {
          title: "订单完成",
          icon: "https://media.wxcwy.com/mini/images/jycg_img@2x.png",
        },
        4: {
          title: "订单取消",
          icon: "https://media.wxcwy.com/mini/images/ygb@2x.png",
        },
      },
      timeData: {},
      wxMerchId: ''
    };
  },
  watch: {
    isPay(n) {
      this.payType = n ? 2 : 1;
    },
  },
  computed: {
    ...mapState(["userInfo"]),
    isPay() {
      if (!this.pageData.amount_paid) return;
      return this.pageData.amount_paid > this.userInfo.amount;
    },
    payTypeText() {
      if (this.payType == 0) return;
      return this.payTypeList.find((item) => item.type == this.payType).name;
    },
  },
  onLoad(options) {
    productId = options.id;
  },
  onShow() {
    this.$api.getWxMerchId().then((data) => {
      this.wxMerchId = data.data;
    });
    this.getOrderDetail();
  },
  methods: {
    onSelectPayType(type) {
      this.payType = type;
      this.showPayType = false;
    },
    onReceipt(order_no, pay_type) {
      // if(pay_type != 2){
          uni.showModal({
          title: "提示",
          content: "是否确认收货?",
          success: (res) => {
            if (res.confirm) {
              this.$api.postOrderConfirm({ order_no }).then((data) => {
                if (data.code == 200) {
                  this.getOrderDetail();
                }
              });
            } else if (res.cancel) {
              console.log("用户点击取消");
            }
          },
        });
      // }else{
      //    //拉起确认收货组件
      //   if (wx.openBusinessView) {
      //     wx.openBusinessView({
      //       businessType: 'weappOrderConfirm',
      //       extraData: {
      //         merchant_id: this.wxMerchId,
      //         merchant_trade_no: order_no,
      //         // transaction_id: ''
      //       },
      //       success() {
      //         uni.showLoading({
      //           title: "确认中",
      //           mask: true,
      //         });
      //         setTimeout(() => {
      //           uni.hideLoading();
      //           this.getOrderDetail();
      //         }, 1500);
      //       },
      //       fail() {
      //         //dosomething
      //       },
      //       complete() {
      //         //dosomething
      //       }
      //     });
      //   } 
      // }
    },
    makePhoneCall(tel) {
      wx.makePhoneCall({
        phoneNumber: tel,
      });
    },
    callOrder(order_no) {
      uni.showModal({
        title: "提示",
        content: "您确定要取消吗？",
        success: (res) => {
          if (res.confirm) {
            this.$api.postOrderCancel({ order_no }).then((res) => {
              if (res.code == 200) {
                this.getOrderDetail();
                uni.showToast({
                  title: "订单已取消",
                  duration: 2000,
                  icon: "none",
                });
              }
            });
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
    async onPay(id) {
      const isPay = await shbPay(
        id,
        this.pageData.amount_paid,
        "",
        this.payType
      );
      if (isPay) {
        this.getOrderDetail();
      }
    },
    onCountDowChange(e) {
      this.timeData = e.detail;
    },
    setClipboardData(text) {
      wx.setClipboardData({
        data: text,
      });
    },
    getOrderDetail() {
      this.$api.getOrderDetail({ id: productId }).then((res) => {
        if (res.code == 200) {
          this.pageData = res.data;
        }
      });
    },
  },
};
</script>

<style lang="scss">
.order-paid {
  min-height: 100vh;
  background: #f4f4f4;
  color: #333333;
  font-size: 30rpx;
  overflow: hidden;
  .van-cell {
    &::after {
      left: 0;
    }
  }
  .img-size-100 {
    widows: 100%;
    height: 100%;
    object-fit: cover;
  }
  .state-box {
    height: 200rpx;
    display: flex;
    align-items: center;
    overflow: hidden;
    background: linear-gradient(
      270deg,
      rgba(44, 172, 255, 1) 0%,
      rgba(40, 123, 252, 1) 100%
    );
    color: #ffffff;
    font-size: 26rpx;
    line-height: 38rpx;
    box-sizing: border-box;
    padding: 0 40rpx 20rpx;
    position: relative;
    .order-status-countTime {
      color: #fff;
    }
    .pic {
      flex: 0 0 116rpx;
      height: 108rpx;
      margin-right: 20rpx;
      overflow: hidden;
      // background: url(https://media.wxcwy.com/mini/images/dfh_img@2x.png) no-repeat left top;
      // background-size: 100% 100%;
    }
    .txt {
      .name {
        margin: 5rpx 0 15rpx;
        font-size: 32rpx;
        line-height: 45rpx;
        font-weight: bold;
      }
    }
    .seat {
      width: 100%;
      height: 30rpx;
      position: absolute;
      bottom: 0;
      left: 0;
      background: #fff;
      border-radius: 40rpx 40rpx 0 0;
      overflow: hidden;
    }
  }
  .info-box {
    overflow: hidden;
    background: #fff;
    padding: 0 30rpx;
    border-radius: 0 0 40rpx 40rpx;
    .address {
      padding: 5rpx 0 40rpx;
      overflow: hidden;
      border-bottom: 1px solid #e7e7e7;
      .top {
        font-size: 32rpx;
        line-height: 45rpx;
        margin-bottom: 15rpx;
        .tel {
          color: #999;
          margin-left: 10rpx;
        }
      }
      .logistics {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 30rpx;
        height: 44rpx;
        .left {
          color: #666666;
          font-size: 26rpx;
          background: url(https://media.wxcwy.com/mini/images/dwbl_icon_wl@2x.png) no-repeat left top;
          background-size: 32rpx 32rpx;
          padding-left: 40rpx;
        }
        .copy-btn {
          box-sizing: border-box;
          height: 44rpx;
          line-height: 44rpx;
          border: 1px solid #999999;
          border-radius: 8rpx;
          overflow: hidden;
          color: #999999;
          font-size: 24rpx;
          padding: 0 25rpx;
        }
      }
      .bottom {
        font-size: 26rpx;
        line-height: 38rpx;
        background: url(https://media.wxcwy.com/mini/images/dwbl_icon@2x.png) no-repeat left top;
        background-size: 32rpx 32rpx;
        padding-left: 40rpx;
      }
    }
    .product-box {
      padding: 30rpx 0;
      overflow: hidden;
      border-bottom: 1px solid #e7e7e7;
      .top {
        height: 56rpx;
        display: flex;
        align-items: center;
        margin-bottom: 30rpx;
        .pic {
          flex: 0 0 56rpx;
          height: 56rpx;
          margin-right: 20rpx;
          border-radius: 50%;
          overflow: hidden;
        }
        .name {
          max-width: 600rpx;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          flex: 1;
          background: url(https://media.wxcwy.com/mini/images/more_arrow@2x.png) no-repeat right center;
          background-size: 25rpx 30rpx;
          padding-right: 35rpx;
        }
      }
      .info {
        display: flex;
        overflow: hidden;
        .pic {
          flex: 0 0 180rpx;
          height: 160rpx;
          border-radius: 8rpx;
          overflow: hidden;
        }
        .name {
          flex: 0 0 315rpx;
          margin: 0 20rpx;
          line-height: 36rpx;
          height: 72rpx;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
        .specification {
          color: #999;
          font-size: 24rpx;
          line-height: 32rpx;
          margin-top: 12rpx;
          padding: 0 18rpx;
        }
        .price {
          height: 36rpx;
          line-height: 36rpx;
          flex: 0 0 145rpx;
          text-align: right;
          .money {
            font-size: 24rpx;
            .number {
              font-size: 32rpx;
            }
          }
          .amount {
            line-height: 32rpx;
            color: #999999;
            font-size: 24rpx;
          }
        }
      }
      .total-tips {
        margin-top: 20rpx;
        font-size: 24rpx;
        line-height: 32rpx;
        overflow: hidden;
        text-align: right;
        .number {
          font-size: 32rpx;
        }
      }
    }
    .list-box {
      overflow: hidden;
      .remark {
        color: #999;
        margin-left: 34rpx;
        font-size: 28rpx;
      }
      .item {
        height: 100rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #e7e7e7;
        .arrow-icon {
          flex: 0 0 25rpx;
          height: 30rpx;
          background: url(https://media.wxcwy.com/mini/images/more_arrow@2x.png) no-repeat center
            center;
          background-size: 100% 100%;
          margin: 0 16rpx;
        }
        &.details {
          justify-content: space-between;
        }
        &:last-child {
          border-bottom: none;
        }
        .input {
          flex: 1;
          height: 40rpx;
          margin-left: 34rpx;
        }
        .number {
          color: #ff7900;
          font-size: 44rpx;
          font-weight: bold;
        }
      }
    }
  }
  .cell-box {
    margin-top: 20rpx;
    padding: 0 30rpx;
    box-sizing: border-box;
    background: #fff;
    .code-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 100rpx;
      box-sizing: border-box;
      color: #333333;
      font-size: 30rpx;
      border-bottom: 1px solid #e7e7e7;
      .coup-icon {
        flex: 0 0 43rpx;
        height: 43rpx;
        background: url(https://media.wxcwy.com/mini/images/qbicon@2x.png) no-repeat;
        background-size: 100%;
        margin-right: 10rpx;
      }
      &.blue {
        color: #1772ff;
      }
      &:last-child {
        border: none;
      }
      .right {
        flex: 0 0 75rpx;
        height: 40rpx;
        background: url(https://media.wxcwy.com/mini/images/more_arrow@2x.png) no-repeat right center;
        background-size: 25rpx 30rpx;
        padding-right: 35rpx;
        box-sizing: border-box;
        .code {
          width: 40rpx;
          height: 40rpx;
          overflow: hidden;
        }
      }
    }
  }

  .order-box {
    background: #fff;
    margin: 20rpx 0;
    padding: 30rpx;
    overflow: hidden;
    .main {
      margin-top: 25rpx;
      overflow: hidden;
      display: flex;
      justify-content: space-between;
      color: #666666;
      font-size: 26rpx;
      line-height: 38rpx;
      .txt {
        margin-top: 10rpx;
      }
      .right {
        .copy-btn {
          box-sizing: border-box;
          height: 44rpx;
          line-height: 44rpx;
          border: 1px solid #999999;
          border-radius: 8rpx;
          overflow: hidden;
          color: #999999;
          font-size: 24rpx;
          padding: 0 25rpx;
        }
      }
    }
  }
  .contact-box {
    margin-top: 20rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100rpx;
    background: #fff;
    color: #333333;
    font-size: 30rpx;
    .icon {
      flex: 0 0 36rpx;
      height: 44rpx;
      background: url(https://media.wxcwy.com/mini/images/yingye_tel-icon@2x.png) no-repeat right
        center;
      background-size: 100% 100%;
      margin-right: 15rpx;
    }
  }
  .footer-box {
    height: calc(98rpx + constant(safe-area-inset-bottom));
    height: calc(98rpx + env(safe-area-inset-bottom));
    .main {
      position: fixed;
      left: 0;
      bottom: 0;
      width: 100%;
      padding-top: 20rpx;
      height: calc(98rpx + constant(safe-area-inset-bottom));
      height: calc(98rpx + env(safe-area-inset-bottom));
      background: #fff;
      display: flex;
      justify-content: flex-end;
      align-items: flex-start;
      box-sizing: border-box;
      .btn {
        width: 170rpx;
        height: 60rpx;
        line-height: 60rpx;
        border-radius: 30rpx;
        border: 1px solid #999999;
        text-align: center;
        margin-right: 20rpx;
        &.pay {
          border-color: #1772ff;
          color: #1772ff;
          font-size: 32rpx;
        }
      }
    }
  }
  .pay-type-pop {
    .cell-box {
      padding: 26rpx 0;
    }
    .model-title {
      color: #999;
      font-size: 28rpx;
      padding: 10rpx 0;
    }
    .title {
      color: #333333;
      font-size: 32rpx;
      margin-left: 30rpx;
      .tips-t {
        color: #b2b2b2;
      }
    }
    .wx-icon {
      width: 64rpx;
      height: 64rpx;
      background: url(https://media.wxcwy.com/mini/images/vx_icon2@2x.png) no-repeat;
      background-size: 100%;
    }
    .ye-icon {
      width: 64rpx;
      height: 64rpx;
      background: url(https://media.wxcwy.com/mini/images/shb_icon@2x.png) no-repeat;
      background-size: 100%;
    }
    .radio-icon {
      width: 36rpx;
      height: 36rpx;
      background: url(https://media.wxcwy.com/mini/images/zfselg_icon@2x.png) no-repeat;
      background-size: 100% 100%;
      &.active {
        background: url(https://media.wxcwy.com/mini/images/rzget_icon@2x.png) no-repeat;
        background-size: 100% 100%;
      }
    }
  }
}
</style>
